package com.demo.io;

import java.io.*;

public class OutputStreamTest {

    public static void main(String[] args) {
//        try {
//            test1();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        inputSreamTest();

    }

    public static void test1() throws IOException {
        File file = new File("F:\\java学习\\基础班\\图片\\a.txt");

        boolean directory = file.isDirectory();
        System.out.println(directory);
        String parent = file.getParent();
        System.out.println(parent);

        File parentFile = file.getParentFile();
        System.out.println(parentFile);
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        OutputStream os = new FileOutputStream(file,true);
        os.write("hello".getBytes());
        os.close();

    }

    public static void inputSreamTest() {
        InputStream is = null;
        OutputStream os = null;
        try {
            File file = new File("F:\\java学习\\基础班\\图片\\a.txt");
            is = new FileInputStream(file);
            os = new FileOutputStream(new File("F:\\java学习\\基础班\\图片\\b.txt"));
            int read = is.read();
            System.out.println(read);
            byte[] bytes = new byte[1024];
            int read1 = 0;
            System.out.println(read1);
            while ((read1 = is.read(bytes)) != -1){
                os.write(bytes,0,read1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //复制目录及子目录 (多层复制)
    public static void copy(File srcFile,File descFile){
        String fileName = srcFile.getName();
        InputStream is = null;
        OutputStream os = null;
        File copyFile = new File(descFile,fileName);
        copyFile.mkdir();
        File[] listFiles = srcFile.listFiles();
        for (File f:listFiles) {

            if(srcFile.isFile()){
                try{
                    is = new FileInputStream(srcFile);
                    os = new FileOutputStream(descFile);

                    byte[] bytes = new byte[1024];
                    int read1 = -1;
                    while ((read1 = is.read(bytes)) != -1){
                        os.write(bytes,0,read1);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (os != null) {
                        try {
                            os.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            if(srcFile.isDirectory()){
                copy(f,copyFile);
            }
        }
    }
}
